/**
 * @Author Guy Franco
 *
 * 	this is the Approve Extention Panel able to search relevant extension requests
 * and approve or deny them
 */

package gui;
import javax.swing.JPanel;

import common.ChatIF;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;


public class ApproveExtentionPanel  extends JPanel implements ChatIF {
	private AcademicFrame frame;
	private ApproveExtentionPanel approveExtentionPanel;
	private SupervisorPanel supervisorPanel;
	private JComboBox ercomboBox;
	
	
	public ApproveExtentionPanel(AcademicFrame frame, SupervisorPanel supervisorPanel) {
		super();
		this.frame=frame;
		this.supervisorPanel=supervisorPanel;
		this.approveExtentionPanel=this;
		frame.setSize(500, 400);
		initialize();
	
	}
		private void initialize()
		{
		
		setLayout(null);
		
		JLabel lblApproveExtention = new JLabel("Approve Extention");
		lblApproveExtention.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblApproveExtention.setForeground(Color.BLUE);
		lblApproveExtention.setBounds(125, 11, 209, 22);
		add(lblApproveExtention);
		
		JLabel lblRelevantExtentionRequests = new JLabel("Relevant Extention Requests");
		lblRelevantExtentionRequests.setBounds(41, 94, 178, 14);
		add(lblRelevantExtentionRequests);
		
		ercomboBox = new JComboBox();
		ercomboBox.setBounds(59, 119, 140, 20);
		add(ercomboBox);
		
		JButton btnDetails = new JButton("Details");
		btnDetails.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(ercomboBox.getSelectedItem().equals("--Choose an Extention--"))// filter invalid choice
				{
					(new Dialog("Please choose an extention request from the list first.")).setVisible(true);
				}
				else{
					ArrayList<Object> extendInfo = new ArrayList<Object>();
					String currentExtID = ercomboBox.getSelectedItem().toString();
					extendInfo.add("getExtendinfo");
					extendInfo.add(currentExtID);
					AcademicFrame.myclient.sendMessageToServer(extendInfo); // get requests
				}
			}
		});
		btnDetails.setBounds(245, 116, 89, 23);
		add(btnDetails);
		
		JButton btnApprove = new JButton("Approve");
		btnApprove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ArrayList<Object> approvExt= new ArrayList<Object>();
				approvExt.add("approvDenyExt");
				approvExt.add("1");
				approvExt.add(ercomboBox.getSelectedItem().toString());
				AcademicFrame.myclient.sendMessageToServer(approvExt); // get requests
			}
		});
		btnApprove.setBounds(41, 240, 89, 23);
		add(btnApprove);
		
		JButton btnDeny = new JButton("Deny");
		btnDeny.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ArrayList<Object> approvExt= new ArrayList<Object>();
				approvExt.add("approvDenyExt");
				approvExt.add("0");
				approvExt.add(ercomboBox.getSelectedItem().toString());
				AcademicFrame.myclient.sendMessageToServer(approvExt); // get requests
			}
		});
		btnDeny.setBounds(185, 240, 89, 23);
		add(btnDeny);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setSize(500, 500);
				frame.setContentPane(supervisorPanel);
			}
		});
		btnCancel.setBounds(324, 240, 89, 23);
		add(btnCancel);
	}

	@Override
	public void display(Object message) {
		// TODO Auto-generated method stub
		ArrayList<Object> answer=new ArrayList<Object>();
		answer.clear(); // clear the info array list
		answer = (ArrayList<Object>)message;
		
		if(answer.get(0).equals("extendlist"))
		{
			if(answer.get(2).equals("0"))
			{
				(new Dialog("No Extentions Available")).setVisible(true);
				frame.setSize(500, 500);
				frame.setContentPane(supervisorPanel);
			}
			else
			{
			int numberOfSystems = Integer.parseInt((answer.get(1)).toString()); // get the number of systems in the list
			this.ercomboBox.addItem("--Choose an Extention--");
			for (int i=0; i < numberOfSystems ; i++ )
			{
				this.ercomboBox.addItem((String)answer.get(i+2));  // add systems to the list
				
			}
			}
		}
		else
			if(answer.get(0).equals("extendInfo"))
			{
				(new DialogXL(" Reason:  "+answer.get(1)+" \n Requested Deadline:  "+answer.get(2))).setVisible(true);
				
			}
			else
				if(answer.get(0).equals("approvExt"))
				{
					(new Dialog("Extention Approved")).setVisible(true);
					frame.setContentPane(new ApproveExtentionPanel(frame, supervisorPanel));
					frame.setSize(460, 420);
					
					ArrayList<Object> extend= new ArrayList<Object>();
					extend.add("getExtend");
					AcademicFrame.myclient.sendMessageToServer(extend); // get requests
					
				}
				else
					if(answer.get(0).equals("DenyExt"))
					{
						(new Dialog("Extention Denied")).setVisible(true);
						frame.setContentPane(new ApproveExtentionPanel(frame, supervisorPanel));
						frame.setSize(460, 420);
						
						ArrayList<Object> extend= new ArrayList<Object>();
						extend.add("getExtend");
						AcademicFrame.myclient.sendMessageToServer(extend); // get requests
						
					}
			
		
	}

}
